<?php

use console\db\Migration;
use yii\db\Query;

/**
 * 年票订单交易信息更新
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m300200_100031_t_card_authent extends Migration {

    /**
     * @inheritdoc
     */
    public $tableName = '{{%user_card}}';

    /**
     * @inheritdoc
     */
    public function safeUp() {
        $query = (new Query())->from($this->tableName)->andWhere([
            'status' => 10,
            'deleted_at' => 0,
        ]);
        $this->handleProgress($query, [$this, 'evenDule']);
    }

    /**
     * @inheritdoc
     */
    public function safeDown() {
        
    }

    /**
     * 处理结果
     * @param Migration $migration
     * @param array $rows
     */
    public static function evenDule($migration, $rows) {
        if (empty($rows)) {
            return;
        }
        $connect = $migration->db->createCommand();
        foreach ($rows as $row) {
            $connect->update('{{%user_authent}}', [
                'status' => 10
            ], [
                'and',
                ['user_id' => $row['user_id']],
                ['!=', 'status', 10]
            ])->execute();
        }
    }

}
